<html>
<head><meta charset="utf-8"><title>Can I constrain a type parameter to be an owned value? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html">Can I constrain a type parameter to be an owned value?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211239823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211239823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211239823">(Sep 25 2020 at 10:13)</a>:</h4>
<p>Can I constrain a type parameter to be an owned value (and not a reference) in a function signature?</p>



<a name="211239887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211239887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211239887">(Sep 25 2020 at 10:14)</a>:</h4>
<p>I think the answer is no.</p>



<a name="211239959"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211239959" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211239959">(Sep 25 2020 at 10:15)</a>:</h4>
<p>I think not, what do you need this for</p>



<a name="211239994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211239994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211239994">(Sep 25 2020 at 10:15)</a>:</h4>
<p>We can theoretically add a trait into the compiler which is implemented for everything except references</p>



<a name="211240017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211240017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211240017">(Sep 25 2020 at 10:15)</a>:</h4>
<p>so if there is something which can't be solved otherwise that seems like a potentially worthwhile addition</p>



<a name="211240767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211240767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211240767">(Sep 25 2020 at 10:25)</a>:</h4>
<blockquote>
<p>what do you need this for</p>
</blockquote>
<p>I think this <em>might</em> be useful for our tracing JIT system, where we want the user to pass us an owned value because we assume a certain ABI for trace compilation.</p>
<p>It's hard to explain, but may be useful in a wider context too? Maybe it's a bit niche?</p>
<p>Conversely, can you constrain that a type parameter <em>is</em> a reference? That might be useful too?</p>



<a name="211241004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241004">(Sep 25 2020 at 10:28)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">trait</span><span class="w"> </span><span class="n">IsReference</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">IsReference</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;</span><span class="n">T</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">IsReference</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">T</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="211241021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241021">(Sep 25 2020 at 10:28)</a>:</h4>
<blockquote>
<p>owned value</p>
</blockquote>
<p>That's not really what references are though, are they?</p>



<a name="211241033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241033">(Sep 25 2020 at 10:28)</a>:</h4>
<p>Is <code>struct Foo&lt;'a, T&gt;(&amp;'a T)</code> an owned value?</p>



<a name="211241075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241075">(Sep 25 2020 at 10:29)</a>:</h4>
<p>It might make more sense to maybe restrict the inputs to <code>'static</code></p>



<a name="211241083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241083">(Sep 25 2020 at 10:29)</a>:</h4>
<p>meaning that can can't borrow anything</p>



<a name="211241230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241230">(Sep 25 2020 at 10:31)</a>:</h4>
<p>I'm thinking more about function boundaries:</p>
<div class="codehilite"><pre><span></span><code>fn f&lt;T&gt;(t: T) -&gt; {...}
</code></pre></div>


<p>And constrain <code>T</code> to be either a reference or an owned value (regardless of what is actually inside <code>T</code>).</p>



<a name="211241295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241295">(Sep 25 2020 at 10:32)</a>:</h4>
<p>yeah, but what do you mean with "owned value"?</p>



<a name="211241308"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241308" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241308">(Sep 25 2020 at 10:32)</a>:</h4>
<p>are these just things with a <code>'static</code> bound?</p>



<a name="211241347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241347">(Sep 25 2020 at 10:33)</a>:</h4>
<p>if you can't clearly define it, can you give some examples of what's owned and what is not owned in your mind?</p>



<a name="211241489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241489">(Sep 25 2020 at 10:34)</a>:</h4>
<p>Maybe my mental model is wrong.</p>
<p>I'd say if <code>T</code> was of type <code>S</code> where <code>S</code> is a struct, it's owned (regardless of if it contains references).</p>
<p>I'd say if <code>T</code> was of type <code>&amp;S</code> where <code>S</code> is a struct, it's not owned.</p>



<a name="211241623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241623">(Sep 25 2020 at 10:36)</a>:</h4>
<p>hmm, then about " where we want the user to pass us an owned value because we assume a certain ABI for trace compilation"</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[repr(transparent)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Foo</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">a</span><span class="w"> </span><span class="nb">String</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="211241641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241641">(Sep 25 2020 at 10:36)</a>:</h4>
<p>With your definition <code>Foo</code> would be an owned value but have the exact same ABI as <code>&amp;'a String</code></p>



<a name="211241728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241728">(Sep 25 2020 at 10:38)</a>:</h4>
<p>from what I can tell this seems a bit like an XY problem for me, but I might be misunderstanding what exactly you are trying to do here</p>



<a name="211241743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Can%20I%20constrain%20a%20type%20parameter%20to%20be%20an%20owned%20value%3F/near/211241743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edd Barrett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Can.20I.20constrain.20a.20type.20parameter.20to.20be.20an.20owned.20value.3F.html#211241743">(Sep 25 2020 at 10:38)</a>:</h4>
<p>I think I need to think about it a bit more myself.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>